package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.TableTypeListBean;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class SelectType2Adapter extends RecyclerView.Adapter<SelectType2Adapter.ViewHolder> {
    private Activity activity;
    private List<TableTypeListBean.DataBean> list;

    public SelectType2Adapter(Activity activity, List<TableTypeListBean.DataBean> list) {
        this.activity = activity;
        this.list = list;
    }

    @Override
    public SelectType2Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type_select, parent, false);
        SelectType2Adapter.ViewHolder holder = new SelectType2Adapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(SelectType2Adapter.ViewHolder holder, final int i) {
        TableTypeListBean.DataBean tableTypeListBean = list.get(i);
        holder.tv_name.setText(tableTypeListBean.getName());
        if (tableTypeListBean.isCheck()) {
            holder.tv_name.setTextColor(activity.getResources().getColor(R.color.white));
            holder.tv_name.setBackgroundResource(R.color.blue_5e);
        } else {
            holder.tv_name.setTextColor(activity.getResources().getColor(R.color.black));
            holder.tv_name.setBackgroundResource(R.color.white);
        }
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tableTypeListBean.setCheck(true);
                for (TableTypeListBean.DataBean dataBean :list){
                    if (!dataBean.getName().equals(tableTypeListBean.getName())){
                        dataBean.setCheck(false);
                    }
                }
                notifyDataSetChanged();
                if (listener != null){
                    listener.onItemClick(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        RelativeLayout ll_item;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            ll_item = itemView.findViewById(R.id.ll_item);
        }
    }
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick( int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
